﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace OCRTheNewHope
{
    public static class Visualizer
    {
        static Pen p = new Pen(new SolidBrush(Color.Red));

        private static int pixelWidth = 10;
        private static int pixelHeight = 10;

        public static Bitmap Visualize(double[,] matrix, Rectangle rect)
        { 
            Bitmap bmp = new Bitmap(matrix.GetLength(0) * pixelWidth, matrix.GetLength(1) * pixelHeight);
            Graphics g = Graphics.FromImage(bmp);

            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    int top = pixelHeight * i;
                    int left = pixelWidth * j;

                    int temp = 255 - (byte)matrix[i, j];
                    Color c = Color.FromArgb(temp, temp, temp);

                    g.FillRectangle(new SolidBrush(c), new System.Drawing.Rectangle(left, top, pixelWidth, pixelHeight));
                }
            }

            DrawLine(g, rect.A.X , rect.A.Y, rect.B.X, rect.B.Y);
            DrawLine(g, rect.B.X, rect.B.Y, rect.D.X, rect.D.Y);
            DrawLine(g, rect.D.X, rect.D.Y, rect.C.X, rect.C.Y);
            DrawLine(g, rect.C.X, rect.C.Y, rect.A.X, rect.A.Y);

            return bmp;
        }

        private static void DrawLine(Graphics g, int x1, int y1, int x2, int y2)
        {
            g.DrawLine(p, x1 * pixelWidth, y1 * pixelHeight, x2 * pixelWidth, y2 * pixelHeight);
        }
    }
}
